<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%
Object obj=request.getSession().getAttribute("role_resource");
if(null==obj){
	response.setStatus(1000);
	return;
}
%>
<script type="text/javascript">
//查询用户信息
		var store = new Ext.data.JsonStore({
			root: "data",//从返回的json对象data中读取数据
			fields: ["id", "name","mobile","phone","email","duty","state","remark",
			"depNo", "depName","orgNo","orgName","roleNo","roleName","loginName"],
  			url:"person!listByDepart.action"//根据部门查询用户信息
    	});
    	//根据部门获取角色信息
		var store2 = new Ext.data.JsonStore({
			root: "data",//从返回的json对象data中读取数据
			fields: ["rid","rname"],
  			url:"role!listByDepID.action"
    	});
		//状态栏选项
		var store3 = new Ext.data.JsonStore({
			fields: ["id","text"],
  			data:[{id:"1",text:"启用"},{id:"0",text:"禁用"}]
    	});
		
    	var reqtype="";
    	function request(){
    		var ajaxurl="";
    		var par={};
    		if("del"==reqtype){
    		//获取列表中被选择的数据
    		var record=Ext.getCmp("usergrid").getSelectionModel().getSelected();
				if(record){
		   			ajaxurl="person!del.action";
	    			par={
	    				id:record.data.id
	    			};
				}else{
					return;
				}
    		}
    		else{
	    		var form=Ext.getCmp("p2form");
	    		//获取原始数据
	    		var formData=form.showData;
	    		//如果为添加操作
	    		if("add"==reqtype){
					ajaxurl="person!add.action";
	  				par={
	  					"loginName":form.getComponent("loginName").getValue(),
	  					"password":form.getComponent("password").getValue(),
	  					"name":form.getComponent("name").getValue(),
						"mobile":form.getComponent("mobile").getValue(),
						"phone":form.getComponent("phone").getValue(),
						"roleNo":form.getComponent("rname").getValue(),//角色ID
						"email":form.getComponent("email").getValue(),
						"duty":form.getComponent("duty").getValue(),
						"remark":form.getComponent("remark").getValue(),
						"depNo":form.getComponent("id").getValue()//部门ID
					};
	    		}//如果为修改操作
	    		else if("edit"==reqtype){
					ajaxurl="person!update.action";
	    			par={
	    				"id":form.getComponent("id").getValue(),
	  					"name":form.getComponent("name").getValue(),
	  					"loginName":form.getComponent("loginName").getValue(),
	  					"password":form.getComponent("password").getValue(),
						"mobile":form.getComponent("mobile").getValue(),
						"phone":form.getComponent("phone").getValue(),
						"roleNo":form.getComponent("rname").getValue(),//角色ID
						"email":form.getComponent("email").getValue(),
						"duty":form.getComponent("duty").getValue(),
						"remark":form.getComponent("remark").getValue(),
						"state":form.getComponent("state").getValue(),
						"depNo":formData.depNo//所属部门
					};
	    		}else if("del"==reqtype){
	    			ajaxurl="person!del.action";
	    			par={
	    				id:form.getComponent('id').getValue()
	    			};
	    		}
    		}
			Ext.Ajax.request({
				clientValidation: true,
				params:par,
				url:ajaxurl,
				success: function(response){
					var d=eval("("+response.responseText+")");
					if(d.code == "0"){
						//Ext.Msg.alert("成功","操作成功!");
						Ext.Msg.show({
							   title:"提示",
							   msg:"操作成功！",
							   buttons:Ext.Msg.OK,
							   icon: Ext.MessageBox.INFO
							});
						Ext.getCmp("user_paging").doRefresh();
						Ext.getCmp("person_panel").layout.setActiveItem(0);//显示第一界面
					} else {
						Ext.Msg.show({
							   title:"提示",
							   msg:d.data,
							   buttons:Ext.Msg.OK,
							   icon: Ext.MessageBox.ERROR
						});
					}
					
					Ext.getCmp("save").setDisabled(false);
				}
			});
    	}
    	
		var p1 = {
			id: "usergrid",
			xtype: "grid",
			stripeRows:true,
			loadMask : true,//在加载数据时遮罩住元素,替换元素Updater的加载提示器，并且在初始化加载之后销毁
			autoExpandColumn:"remark",//自动填充
			autoExpandMin : 100,//自动填充最小宽度
			store:store,
			headersDisabled: true,
			columns:[
	        	new Ext.grid.RowNumberer({header: "序号", width: 30}),
	        	{header: "人员编号", dataIndex: "id", menuDisabled: true},
	        	{header: "人员名称", dataIndex: "name", menuDisabled: true},
	        	{header: "角色名称", dataIndex: "roleName", menuDisabled: true},
				{header: "部门名称", dataIndex: "depName", menuDisabled: true},
				{header: "结构名称", dataIndex: "orgName", menuDisabled: true},
				{header: "备注",dataIndex:"remark",id:"remark", menuDisabled: true}
			],
			bbar: new Ext.ux.PagingToolbar({
				id:"user_paging",
	        	pageSize:limit,
	        	store:store
    		}),
        	buttonAlign:"center",
			buttons:[{text:"新增", handler: function(){
				reqtype="add";
				var node=Ext.getCmp("roleTree").getSelectionModel().getSelectedNode();
				if(node!=null && node.isLeaf()){
					Ext.getCmp("person_panel").layout.setActiveItem(1);
					var form=Ext.getCmp("p2form");
					form.resetParam(node.text,node.parentNode.text);
					form.getComponent("id").setValue(node.id);
					form.getComponent("id").setVisible(false);
					form.getComponent("state").setVisible(false);//不显示状态
				}
				else{
					//Ext.Msg.alert("提示","请选择人员所属的部门!");
					Ext.Msg.show({
						   title:"警告",
						   msg: "请选择人员所属的部门!",
						   buttons: Ext.Msg.OK,
						   animEl: "elId",
						   icon:Ext.MessageBox.WARNING
						});
				}
			}},{text:"修改", handler: function(){
				reqtype="edit";
				var node=Ext.getCmp("roleTree").getSelectionModel().getSelectedNode();
				var form=Ext.getCmp("p2form");
				form.getComponent("id").setVisible(true);//显示ID
				form.getComponent("state").setVisible(true);//显示ID
				//获取列表中被选择的数据
				var record=Ext.getCmp("usergrid").getSelectionModel().getSelected();
				if(record){
					Ext.getCmp("person_panel").layout.setActiveItem(1);
					Ext.getCmp("p2form").setParam(record.data);
				}else{
					Ext.Msg.show({
						   title:"警告",
						   msg: "请选择需要修改的人员!",
						   buttons: Ext.Msg.OK,
						   animEl: "elId",
						   icon:Ext.MessageBox.WARNING
						});
				}
				
			}},{text:"删除", handler: function(){
				reqtype="del";
				var record=Ext.getCmp("usergrid").getSelectionModel().getSelected();
				if(record){
				   Ext.Msg.show({
				   title:"提示",
				   msg:"是否确认删除该数据?",
				   buttons: Ext.Msg.OKCANCEL,
				   fn:function(btn, text){
					    if (btn == 'ok'){
					    	request();
					    }
				   },
				   animEl:'elId',
				   icon: Ext.MessageBox.WARNING
				});
				}else{
					Ext.Msg.show({
						   title:"警告",
						   msg: "请选择需要删除的人员!",
						   buttons: Ext.Msg.OK,
						   animEl: "elId",
						   icon:Ext.MessageBox.WARNING
						});
				}
				
			}}]
		};
		
		//--------------------------------------------------------------------
		var p2 = {
			id:"p2form",
			title:"用户信息",
		    labelWidth: 60,
			xtype: "form",
		    bodyStyle:"padding:5px 5px 0",
			defaultType: "textfield",
			items: [{
				itemId: "id",
				disabled:true,
				fieldLabel: "人员编号",
				anchor:"100%",
				name: "id"
			},{
				itemId: "name",
				fieldLabel: "人员名称",
				anchor:"100%",
				name: "name",
				vtype:"alphanumchinese",
					validator:function (val){
		        		if(val=="")return "该输入项为必输项！";
		        		if(Ext.fucCheckCHLength(val)>50){
		        			return "请输入50个字符范围内的值！<br>注：一个中文字符算两个字符的长度！";
		        		}
		        		return true;
		        	}
			},{
				id: "loginName",
				fieldLabel: "登陆名",
				anchor:"100%",
				name: "loginName",
				validator:function (val){
	        		if(val=="")return "该输入项为必输项！";
	        		if(Ext.fucCheckCHLength(val)>10){
	        			return "请输入10个字符范围内的值！<br>注：一个中文字符算两个字符的长度！";
	        		}
	        		return true;
	        	},
	        	listeners: {
					'blur': function(){
						var loginN = Ext.getCmp("loginName").getValue();
						if(loginN.length != 0){
							Ext.Ajax.request({
								params: {"loginname": loginN },
								url: 'person!checkPerson.action',
								success: function(response){
									var d=eval("("+response.responseText+")");
									if(d.code == "1"){
										Ext.getCmp("loginName").setValue("");
										Ext.Msg.show({
				   							title:"提示",
				   							msg:"该登陆名已存在，请重新输入！",
				   							buttons:Ext.Msg.OK,
				   							icon: Ext.MessageBox.ERROR
										});
									}
								}
							});
						}
					}
				}
			},{
				itemId: "password",
				fieldLabel: '登陆密码',
				inputType: 'password',
				anchor:'100%',
				name: 'password',
				vtype:'password',
				validator:function (val){
	        		if(val=='' && reqtype!="edit"){
	        			return '该输入项为必输项,修改时输出此字段将会修改密码！';
	        		}
	        		if(Ext.fucCheckCHLength(val)>20){
	        			return '请输入20个字符范围内的值！';
	        		}
	        		return true;
	        	}
			},{
				itemId: "phone",
				xtype:"numberfield",
				fieldLabel: "办公电话",
				anchor:"100%",
				maxValue:999999999999,
				name: "phone"
			},{
				itemId: "mobile",
				fieldLabel: "手机",
				xtype:"numberfield",
				anchor:"100%",
				name: "mobile",
				maxValue:99999999999,
				vtype:'mobilephone'
			},{
				itemId: "email",
				vtype:"email",
				fieldLabel: "邮箱",
				anchor:"100%",
				name: "email",
				validator:function (val){
	        		if(val=="")return true;
	        		if(Ext.fucCheckCHLength(val)>50){
	        			return "请输入50个字符范围内的值！<br>注：一个中文字符算两个字符的长度！";
	        		}
	        		return true;
	        	}
			},{
				itemId: "duty",
				fieldLabel: "职务",
				anchor:"100%",
				vtype:"alphanumchinese",
				name: "duty",
				validator:function (val){
	        		if(val=="")return true;
	        		if(Ext.fucCheckCHLength(val)>20){
	        			return "请输入20个字符范围内的值！<br>注：一个中文字符算两个字符的长度！";
	        		}
	        		return true;
	        	}
			},{
				itemId: "state",
				fieldLabel: "人员状态",
				xtype:"combo",
				anchor:"100%",
				editable:false,//不允许编辑
				valueField:"id",//做为参数的值
				displayField:"text",//做为显示的值
				forceSelection: true,//设置只能从列表中选取
				triggerAction: "all",
				mode:"local",//本地加载模式
				store:store3,
				name:"state"
			},{
				itemId: "oname",
				disabled:true,
				fieldLabel: "机构名称",
				anchor:"100%",
				name: "oname"
			},{
				itemId: "dname",
				disabled:true,
				fieldLabel: "部门名称",
				anchor:"100%",
				name: "dname"
			},{
				itemId: "rname",
				fieldLabel: "角色名称",
				xtype:"combo",
				editable:false,//不允许编辑
				valueField:"rid",//做为参数的值
				displayField:"rname",//做为显示的值
				forceSelection: false,//设置只能从列表中选取
				triggerAction: "all",
				store:store2,
				mode:"local",//本地加载模式
				anchor:"100%",
				name:"rname",
				tpl:'<tpl for="."><div class="x-combo-list-item">{[values.rname == "" ? "&nbsp;" : values.rname]}</div></tpl>'
			},{
				itemId: "remark",
				fieldLabel: "备注",
				anchor:"100%",
				vtype:"alphanumchinese",
				name: "remark",
					validator:function (val){
		        		if(val=="")return true;
		        		if(Ext.fucCheckCHLength(val)>200){
		        			return "请输入200个字符范围内的值！<br>注：一个中文字符算两个字符的长度！";
		        		}
		        		return true;
		        	}
			}],
			buttonAlign:"center",
			buttons:[{text:"保存",id:"save", handler: function(e){
				if(!Ext.getCmp("p2form").getForm().isValid()){
					//Ext.Msg.alert("提示","验证不通过！");
					Ext.Msg.show({
						   title:"提示",
						   msg:"验证不通过!",
						   buttons:Ext.Msg.OK,
						   icon: Ext.MessageBox.ERROR
						});
					return;
				}
				e.setDisabled(true);
				request();
				}},{text:"取消", handler: function(){
					Ext.getCmp("person_panel").layout.setActiveItem(0);//显示第一界面
				}}],
			showData:null,//数据缓存
			setParam: function(data){
				var me = this;
				me.showData=data;
				me.getComponent("id").setValue(data.id);
				me.getComponent("name").setValue(data.name);
				me.getComponent("phone").setValue(data.phone);
				me.getComponent("mobile").setValue(data.mobile);
				me.getComponent("email").setValue(data.email);
				me.getComponent("state").setValue(data.state);
				me.getComponent("oname").setValue(data.orgName);
				me.getComponent("dname").setValue(data.depName);
				me.getComponent("rname").setValue(data.roleNo);
				me.getComponent("duty").setValue(data.duty);
				me.getComponent("remark").setValue(data.remark);//备注
				me.getComponent("loginName").setValue(data.loginName);
				me.getComponent("password").setValue("");
			},
			resetParam: function(depName,orgName){
				var me = this;
				me.getComponent("id").setValue("");
				me.getComponent("name").setValue("");
				me.getComponent("phone").setValue("");
				me.getComponent("mobile").setValue("");
				me.getComponent("email").setValue("");
				me.getComponent("loginName").setValue("");
				me.getComponent("password").setValue("");
				me.getComponent("oname").setValue(orgName);
				me.getComponent("dname").setValue(depName);
				me.getComponent("rname").setValue("");
				me.getComponent("remark").setValue("");//备注
				me.getComponent("duty").setValue("");
			}
		};
		
		var user_pel = new Ext.Panel({
			id:"master_panel",
			border : false, 
			layout: "border",
			items: [{
					id:"roleTree",
					xtype: "billTreePanel",
					dataUrl: "tree!role.action",
					root: new Ext.tree.AsyncTreeNode({
						id:"root",//跟节点ID
						expanded : true//是否展开
					}),
					listeners: {
						"click": function(node){
							Ext.getCmp("person_panel").layout.setActiveItem(0);//显示第一界面
							if(node.isLeaf()){
								store.baseParams["depNo"] = node.id;
								store.load({params:{start : 0,limit :limit}});
								store2.load({params:{"role.did":node.id},callback:function (){
									 var defaultData = {
					                    rid: '',
					                    rname: ''
					                };
					                var recId = store2.getCount()+10; 
					                var p = new store2.recordType(defaultData, recId); 
					                store2.insert(0, p); 
								}});//根据部门ID查询角色
							}
						}
					}
				}, {
					id:"person_panel",
					region: "center",
					border : false, 
					layout:"card",
					activeItem:0,//显示第一个容器
					items: [p1, p2]
				}]
		});	
		
	var CenterPanel=Ext.getCmp("CenterPanel");
	CenterPanel.removeAll();
	CenterPanel.add(user_pel);
	CenterPanel.doLayout();
</script>